package com.kgc.dao;

import com.kgc.entity.Repair;
import com.kgc.entity.Tousu;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserHomeDao extends BaseDao{
    //查询我的投诉账单
    public int totalMyTouSu(Integer id){
        String sql ="SELECT count(*) FROM user u INNER JOIN tousu ts on u.id = ts.user_id WHERE u.id = ?";
        try {
            ResultSet rs = super.getExecuteQuery(sql, id);
            if(rs.next()){
               return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询总投诉账单
    public int totalTouSu(){
        String sql ="SELECT count(*) FROM tousu ";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
               return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询我的维修
    public int totalMyRepair(Integer id){
        String sql ="SELECT count(*) FROM user u INNER JOIN repair r on u.id = r.user_id WHERE u.id = ?";
        try {
            ResultSet rs = super.getExecuteQuery(sql, id);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询总维修数
    public int totalRepair(){
        String sql ="SELECT count(*) FROM repair  ";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询总账单个数
    public int totalPayment(){
        String sql ="SELECT count(*) FROM user u INNER JOIN user_payment up on u.id = up.user_id ";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询我的账单
    public int totalMyPayment(Integer id){
        String sql ="SELECT count(*) FROM user u INNER JOIN user_payment up on u.id = up.user_id where u.id = ?";
        try {
            ResultSet rs = super.getExecuteQuery(sql, id);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //新增投诉
    public int addTouSu(Tousu tousu){
        String sql = "INSERT INTO `tousu`( `content`, `user_id`, `status`, `time`) VALUES (?,?,1,?);";
        return super.getExecuteUpdate(sql,tousu.getContent(),tousu.getUserId(),tousu.getTime());
    }
    //新增维修
    public int addRepair(Repair repair){
        String sql = "INSERT INTO `repair`( `content`, `user_id`, `status`, `time`) VALUES (?,?,0,?);";
        return super.getExecuteUpdate(sql,repair.getContent(),repair.getUserId(),repair.getTime());
    }

}
